﻿using System;
using System.Data;
using System.Web.UI.WebControls;
using BLL;
using Utils;
namespace UI.ModSalary
{
    public partial class SalarySearch : System.Web.UI.Page
    {      
        string account = "";
        DefDicBiz defDicBiz = new DefDicBiz();
        SalaryTableBiz salaryTableBiz = new SalaryTableBiz();  
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["user"] != null)
            {
                User user = Session["user"] as User;
                account = user.Account;
            }
            else
            {
                JSUtility.Alert(this, "您没有登录!请先登录。");
                return;
            }
            if (!IsPostBack)
            {
                new DepartmentBiz().InitDropDownList(ddlDeptID, "0");
                defDicBiz.InitXMLDropDownList(ddlYear, "YearList.xml", System.DateTime.Now.Year.ToString());
                defDicBiz.InitXMLDropDownList(ddlMonth, "MonthList.xml", (System.DateTime.Now.Month - 1).ToString());
                defDicBiz.InitDropDownList("Wage_Sets", ddlSetsID, "1");
                defDicBiz.InitDropDownList("Wage_SalaryType", ddlSalaryType, "0");
            }
        }
        private void Bind()
        {
            string empCode = txtSeWord.Text.Trim();//只查询使用
            string deptName = ddlDeptID.SelectedItem.Text.Trim();
            string strYear = ddlYear.SelectedItem.Text;
            string strMonth = ddlMonth.SelectedItem.Text;
            string setsID = ddlSetsID.SelectedValue;
            string typeID = ddlSalaryType.SelectedValue.ToString();
            if(setsID == "0" )
            {
                JSUtility.Alert(this, "请选择帐套！");
                return;
            }
            if (deptName == "请选择部门") deptName = "";
            if (typeID == "0" &&  deptName == "" && empCode == "")
            {
                JSUtility.Alert(this, "请输入一个查询条件：部门,类别,工号其中一个！");
                return;
            }

            DataTable dt = salaryTableBiz.GetLists(empCode, deptName, strYear, strMonth, setsID, typeID);
            int row = dt.Rows.Count;
            if (row > 0)
            {
                gvList.DataSource = dt;
                gvList.DataBind();
            }
        }
        protected void btnEmSearch_Click(object sender, EventArgs e)
        {
            if (!AccountBiz.CheckRight(account, "SalarySearch.aspx", RightChar.Ser))
            {
                JSUtility.Alert(this, "您没有查询工资的权限！");
                return;
            }
            Bind();
        }
        protected void gvList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow || e.Row.RowType == DataControlRowType.Header)
            {
                e.Row.Cells[e.Row.Cells.Count - 1].Visible = false;               
            }
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                string empCode = e.Row.Cells[2].Text;
                string setsID = ddlSetsID.SelectedValue.ToString();
                string strYear = ddlYear.SelectedItem.Text;
                string strMonth = ddlMonth.SelectedItem.Text;
                e.Row.Cells[2].Text = " <a href=\"javascript:ShowLink('SalaryAdd.aspx?EmpCode=" + empCode + "&SetsID=" + setsID + "&StrYear=" + strYear + "&StrMonth=" + strMonth + "',780,610)\">" + empCode + "</a>";
            }
            //if (e.Row.RowType == DataControlRowType.Footer)
            //{
                
            //    int cols = gvList.HeaderRow.Cells.Count - 1;
            //    for (int i = 5; i < cols; i++)
            //    {
            //        //e.Row.Cells[0].ColumnSpan = 4;
            //       // e.Row.Cells[1].Visible = e.Row.Cells[2].Visible = e.Row.Cells[3].Visible = false;

            //        e.Row.Cells[4].Text = gvList.Rows.Count + "人";
            //        e.Row.Cells[i].Text = Utils.ToolUtility.ReturnTotal(gvList, i).ToString();
            //    }
            //}
        }
      
        protected void btnExcel_Click(object sender, EventArgs e)
        {
            if (!AccountBiz.CheckRight(account, "SalarySearch.aspx", RightChar.Out))
            {
                JSUtility.Alert(this, "您没有导出工资的权限！");
                return;
            }
            string empCode = txtSeWord.Text.Trim();//只查询使用
            string deptName = ddlDeptID.SelectedItem.Text.Trim();
            string strYear = ddlYear.SelectedItem.Text;
            string strMonth = ddlMonth.SelectedItem.Text;
            string setsID = ddlSetsID.SelectedValue;
            string typeID = ddlSalaryType.SelectedValue.ToString();

            //ExcelUtility.GridViewToExcel(gvList, "SalaryTable.xls");
             ExcelUtility.ExportDsToXls(this, salaryTableBiz.GetLists(empCode, deptName, strYear, strMonth, setsID, typeID), "SalaryTable.xls");

        }

        //protected void gvList_PageIndexChanging(object sender, GridViewPageEventArgs e)
        //{
        //    this.gvList.PageIndex = e.NewPageIndex;
        //    Bind();

        //}
    }
}